package com.myexam.action;


import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;







import com.myexam.model.Exam;
import com.myexam.model.Student;
import com.myexam.model.Subject;
import com.myexam.service.TeacherService;
import com.opensymphony.xwork2.ActionSupport;
@Controller
public class GetRandomSubjectAction extends ActionSupport implements RequestAware,SessionAware {


	private static final long serialVersionUID = -8368810171470397763L;
	private Map<String, Object> request;
	private Map<String, Object> session;
	private String exam_id;
	public Map<String, Object> getRequest() {
		return request;
	}

	public void setRequest(Map<String, Object> request) {
		this.request = request;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public String getExam_id() {
		return exam_id;
	}
	public void setExam_id(String exam_id) {
		this.exam_id = exam_id;
	}

	@Autowired
	private TeacherService teacherService;
	
	public String execute () throws Exception{
		Student student = (Student) session.get("studentInfo");
		if (teacherService.exam(Integer.parseInt(student.getStudentID()), Integer.parseInt(exam_id))) {
			List<Subject> list = teacherService.queryExamSubject(Integer.parseInt(exam_id));
			Exam exams = teacherService.queryExam(Integer.parseInt(exam_id));
			int num = 0;
			for (int i = 0; i < list.size(); i++) {
				num +=5;
			}
			session.put("score",num);
			session.put("exam_id", exam_id);
			session.put("time", exams.getExam_use());
			request.put("subjects", list);
			return SUCCESS;
		}else{
			addActionError("本次考试已经考过了！");
			return INPUT;
		}
		
	}
	
	

}
